package ilender.typemappers;

import java.sql.*;

import org.apache.ibatis.type.*;
import org.joda.time.DateMidnight;

import ilender.util.*;

/**Used for converting JodaTime's date to PostgreSQL date and vice versa.*/
public class JodaDateMidnightHandler extends BaseTypeHandler<DateMidnight>{

	public void setNonNullParameter(PreparedStatement ps,int i,DateMidnight parameter,JdbcType jdbcType) throws SQLException{
		ps.setDate(i,new Date(parameter.getMillis()));
	}

	public DateMidnight getNullableResult(ResultSet rs, String columnName) throws SQLException{
		Date date=rs.getDate(columnName);
		if(null!=date)
			return new DateMidnight(date.getTime());
		else
			return null;
	}

	public DateMidnight getNullableResult(ResultSet rs, int columnIndex) throws SQLException{
		Date date=rs.getDate(columnIndex);
		if(null!=date)
			return new DateMidnight(date.getTime());
		else
			return null;
	}

	public DateMidnight getNullableResult(CallableStatement cs, int columnIndex) throws SQLException{
		Date date=cs.getDate(columnIndex);
		if(null!=date)
			return new DateMidnight(date.getTime());
		else
			return null;
	}
}
